﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Permissions;
using System.Windows.Forms;
using CodeLibrary.Consts;

namespace CodeLibrary.IO.Utils
{
    public static class FileUtils
    {
        public static void AppendUTF8Text(string filename, string content)
        {
            using (StreamWriter sw = File.AppendText(filename))
            {
                sw.WriteLine(content);
                sw.Flush();
            }
        }

        public static List<string> ReadTextFile(string filename)
        {
            List<string> list = new List<string>();
            using (StreamReader sr = new StreamReader(filename))
            {
                while (!sr.EndOfStream)
                {
                    list.Add(sr.ReadLine());    
                }
            }
            return list;
        }

        public static string SelectFile(string filter, string initialDirectory)
        {
            OpenFileDialog dialog = new OpenFileDialog {Filter = filter, InitialDirectory = initialDirectory};
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                return dialog.FileName;
            }
            return "";
        }

        public static string SelectFile(string filter)
        {
            return SelectFile(filter, "");
        }

        public static string SaveFile(string filter, string initialDirectory)
        {
            SaveFileDialog dialog = new SaveFileDialog {Filter = filter, InitialDirectory = initialDirectory};
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                return dialog.FileName;
            }
            return "";
        }

        public static string SaveFile(string filter)
        {
            return SaveFile(filter, "");
        }

        public static void MappingPath(string AJunction, string ASrcPath, string ADstPath)
        {

        }

         [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public static void ExecuteFile(string file)
        {
            if (!File.Exists(file))
            {
                throw new FileNotFoundException();
            }
            Process.Start(file);
           
        }

        public static void RecurHandleFiles(string root, FileHandlerChecker checker, FileHandler handler)
        {
            if (root == "" || !Directory.Exists(root))
            {
                throw new ArgumentException(HintConsts.DirectoryNotFound);
            }
            try
            {
                DirectoryInfo di = new DirectoryInfo(root);
                FileInfo[] fis = di.GetFiles();
                foreach (FileInfo fi in fis)
                {
                    if (checker(fi))
                    {
                        handler(fi.FullName);
                    }
                }
                DirectoryInfo[] dis = di.GetDirectories();
                foreach (DirectoryInfo diInfo in dis)
                {
                    RecurHandleFiles(diInfo.FullName, checker, handler);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
    public delegate bool FileHandlerChecker(FileInfo file);
    public delegate void FileHandler(string filename);
}
